// pages/length/length.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['千米 km','米 m', '分米 dm', '厘米 cm','毫米 mm'],
    beIndex: 0,
    index:1,
    beConverted:0,
    converted:0,
    beColor:'black',
    color:'black'
  },
  bindPickerChangeBe: function(e) {
   this.setData({
        beIndex: e.detail.value,
      })   
     let index=this.data.index;
     let beConverted=this.data.beConverted;
     let beIndex=this.data.beIndex;
    if(beIndex===0){
      this.setData({
        converted:beConverted*(10**(index-beIndex+2))
      })
    }else{
      this.setData({
        converted: beConverted*(10**(index-beIndex))
      })
    }
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
    let index=this.data.index;
    let converted=this.data.converted;
    let beIndex=this.data.beIndex;
   if((beIndex===0 || index===0) && beIndex-index!==0){
     this.setData({
       beConverted:converted*(10**(beIndex-index-2))
     })
   }else{
     this.setData({
       beConverted: converted*(10**(beIndex-index))
     })
   }
  },
  beChange:function(e){
  const inputValue = e.detail.value;
  let beIndex=this.data.beIndex;
  let index=this.data.index;
  if(beIndex===0){
    this.setData({
      beConverted:+inputValue,
      converted: inputValue*(10**(index-beIndex+2))
    })
  }else{
    this.setData({
      beConverted:+inputValue,
      converted: inputValue*(10**(index-beIndex))
    })
  }
},
change:function(e){
  const inputValue = e.detail.value;
  let beIndex=this.data.beIndex;
  let index=this.data.index;
  if(beIndex===0){
    this.setData({
      beConverted:inputValue*(10**(beIndex-index-2)),
      converted: +inputValue,
    })
  }else{
    this.setData({
      beConverted:inputValue*(10**(beIndex-index)),
      converted: +inputValue,
    })
  }
},
onFocus:function(e){
  if(e.currentTarget.id==="BeConverted"){
    this.setData({
       beColor:'orange',
    })
  }
  if(e.currentTarget.id==="Converted"){
    this.setData({
      color:'orange',
   })
  }
},
onBlur:function(e){
  if(e.currentTarget.id==="BeConverted"){
    this.setData({
       beColor:'black',
    })
  }
  if(e.currentTarget.id==="Converted"){
    this.setData({
      color:'black',
   })
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})